package com.briup.mapper;

import com.briup.bean.User;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

/**
 * @author pgc
 * @Description:
 * @date 2024/9/29 10:10
 */
@Mapper
public interface UserMapper {
    //#{} 有两个作用
    //1.提供占位符
    //2.写入参数名称，获取传入的值
    //注意，#{}优先找对应类型，然后找名字
    @Delete("delete from es_user where id=#{id}")
    public void deleteUserById(Integer id);

    @Insert("insert into es_user(username,password," +
            "phone,avatar,gender,status,birthday,register_time) " +
            "values(#{username}," +
            "#{password},#{phone},#{avatar},#{gender}," +
            "#{status},#{birthday},#{registerTime})")
    public void insertUser(User user);


    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into es_user(username,password," +
            "phone,avatar,gender,status,birthday,register_time) " +
            "values(#{username}," +
            "#{password},#{phone},#{avatar},#{gender}," +
            "#{status},#{birthday},#{registerTime})")
    public void insertUser1(User user);

    @Update("update es_user set username=#{username}" +
            ",password=#{password}," +
            "phone=#{phone},avatar=#{avatar}," +
            "gender=#{gender},status=#{status}," +
            "birthday=#{birthday}," +
            "register_time=#{registerTime} where id=#{id}")
    public void updateUser(User user);

    @Results({@Result(column = "register_time",property ="registerTime" )})
    @Select("select id,username,password,phone," +
            "avatar,gender,status,birthday," +
            "register_time from es_user where id=#{id}")
    public User selectById(Integer id);

    @Results({@Result(column = "register_time",property ="registerTime" )})
    @Select("select id,username,password,phone," +
            "avatar,gender,status,birthday," +
            "register_time from es_user")
    public List<User> selectAll();

    @Select("select * from es_user where\n" +
            "    username like  concat('%',#{name},'%')\n" +
            "      and status=#{status}\n" +
            "       and\n" +
            "        register_time \n" +
            "\t  between #{start} and #{end}")
    public List<User> queryByCondition(@Param("name") String username,
                                       @Param("status")Byte status,
                                       @Param("start")LocalDate start,
                                       @Param("end")LocalDate end);
}
